<!--
 * @Author: tansq
 * @Date: 2021-09-14 10:18:50
 * @LastEditTime: 2022-11-25 16:13:38
 * @LastEditors: tansq
 * @Description: 地区选择
 * @FilePath: \hte-vue-dev\src\components\tools\AreaSelect.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->
<template>
  <div class='app-container' :class="{isShow:!display}">
      <a-cascader 
        v-model="areaModel"
        placeholder="请选择地区"
        :options="areaData"
        :defaultValue="defaultValue"
        :change-on-select="changeOnSelect"
        :field-names="fieldNames"
        @change="change"
        :show-search="{ filterArea }"
        />
  </div>
</template>

<script>
const splitStr = "/";
  import { httpAction, getAction,getActionAsync } from '@/api/manage'
export default {
  name: 'AreaSelect',
  components: {
  },
  props: {
      defaultValue:{
          type:Array,
          required:false,
          default:()=>{
              return []
          },
      },
      fieldNames:{
          type:Object,
          default:()=>{
              return { label: 'label', value: 'value',children:'children' }
          },
          required:false 
      },
      changeOnSelect:{
          //
        type:Boolean,
        default:false,
        required:false,
      },
      url:{
          type:String,
          default:"/mdmDict/queryArea",
          required:false
      },
      display:{
          type:Boolean,
            default:true,
            required:false

      }
  },
  data() {
    return {
        areaData:[],
        areaModel:[],
        allArea:[]
    };
  },
  watch: {
    //   defaultValue:function(value){
    //       this.areaModel = value
    //   }
  },
  created(){
      this.getArea()
  },
  methods: {
      change(value, selectedOptions){
          
         this.$emit("changeArea",value, selectedOptions,value.join("/"))   
      },

        getArea(){
            getAction(this.url, {  pageNo: 1, pageSize: -1 }).then(res => {
            if (res.success) {
                
                this.areaData= res.result
                let allData = [...res.result]
                let allDataResult = []
                this.handleAllArea(allData,allDataResult)
                this.allArea = allDataResult
                this.$emit("getAllArea",allDataResult)

            }   
    })
  },
    handleAllArea(data,allArr){
        data.forEach((it,i) => {
            allArr.push(it);
            if( !!it.children && it.children.length > 0){
                    this.handleAllArea(it.children,allArr)
            }
        })
    },
    async getAllArea(){
       let allDataResult = []
       this.allArea = allDataResult
       let res =await getActionAsync(this.url, {  pageNo: 1, pageSize: -1 });
      if (res.success) {
          let allData = [...res.result]
          this.handleAllArea(allData,allDataResult)
          this.allArea = allDataResult
          this.$emit("getAllArea",allDataResult)
      } 
      return allDataResult
       
    },
    areArryToStr(arr){
      //数组数据转字符串

    },
    getValueOrLabel(strData,getType = 'label'){
      //getValueOrLable('1/2/3','label')
      //getType :'value' 'label'
      if(!this.allArea || this.allArea.length == 0){
        this.getAllArea()
      }
      
      let findKey = getType == 'label'? 'value':'label'

      
      var arr =  strData.split(splitStr)
      var lableArr = []
      for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          let areaInfo = this.allArea.find(item=>{
              return item[findKey] == element
          })
          if(!!areaInfo){
               lableArr.push(areaInfo[getType]) 
          }
      }
      if(!lableArr || lableArr.length == 0){
         this.$message.warning(`操作异常:数据不存在！`);
         throw "数据不存在！"

      }
      return lableArr.join(splitStr)
    },
    checkDataByLabel(labelStr){
      //错误返回false
      let labelAttr = labelStr.split(splitStr)
      for (let index = 0; index < labelAttr.length; index++) {
        const element = labelAttr[index];
       if(!this.findAreaByLabel(element)) {
          this.$message.warning(`操作异常:${element}不存在！`);
          return false
       }
      }
      return true
    },
    findAreaByLabel(label){
      //查询数据
       let findKey =  'label';
      if(!this.allArea || this.allArea.length == 0){
        this.getAllArea()
      }
       let areaInfo = this.allArea.find(item=>{
              return item[findKey] == label
        })
        return areaInfo
    },
     filterArea(inputValue, path) {
        return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
      },
  },
  computed:{
  },
  filters: {
  },

  mounted(){

  }
 }
</script>

<style scoped>
    .isShow{
        overflow: hidden;
        width: 0;
        height: 0;
        opacity: 0;
    }
</style>
